package wci.frontend.pascal.parsers;

import wci.frontend.*;
import wci.frontend.pascal.*;
import wci.intermediate.*;

import static wci.frontend.pascal.PascalTokenType.*;
import static wci.frontend.pascal.PascalErrorCode.*;
import static wci.intermediate.icodeimpl.ICodeNodeTypeImpl.*;

/**
 * <h1>CompoundStatementParser</h1>
 *
 * <p>解析复合语句</p>
 *
 * <p>Copyright (c) 2009 by Ronald Mak</p>
 * <p>For instructional purposes only.  No warranties.</p>
 */
public class CompoundStatementParser extends StatementParser
{
    public CompoundStatementParser(PascalParserTD parent)
    {
        super(parent);
    }

    /**
     * 解析复合语句，复合语句一定是以BEGIN开头，以END结束.
     * @param token the initial token.
     * @return the root node of the generated parse tree.
     * @throws Exception if an error occurred.
     */
    public ICodeNode parse(Token token)
        throws Exception
    {
        token = nextToken();  //吞噬掉BEGIN

        // 创建复合语句节点节点
        ICodeNode compoundNode = ICodeFactory.createICodeNode(COMPOUND);

        //解析BEGIN ... END中的语句列表
        StatementParser statementParser = new StatementParser(this);
        statementParser.parseList(token, compoundNode, END, MISSING_END);

        return compoundNode;
    }
}
